package com.unlcn.erp.hyc.interfaces.backend.service;

import com.unlcn.erp.hyc.interfaces.backend.enums.CallHYCUrlTypeEnum;
import com.unlcn.erp.hyc.interfaces.base.mapper.HycExceptionMsgMapper;
import com.unlcn.erp.hyc.interfaces.base.mapper.SoshiplineMapper;
import com.unlcn.erp.hyc.interfaces.base.mapper.SoshiplinebackhycMapper;
import com.unlcn.erp.hyc.interfaces.base.model.HycExceptionMsg;
import com.unlcn.erp.hyc.interfaces.base.model.Soshipline;
import com.unlcn.erp.hyc.interfaces.base.model.Soshiplinebackhyc;
import com.unlcn.erp.hyc.interfaces.base.model.bo.HycExceptionMsgBo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by zhiche024 on 2017/9/22.
 */
@Service
public class ExecptionDisposeServiceImpl implements ExecptionDisposeService{

    private static final Logger LOGGER = LoggerFactory.getLogger(ExecptionDisposeServiceImpl.class);

    @Autowired
    private CallWayBillByHYCService callWayBillByHYCService;

    @Autowired
    private CallOrderByHYCService callOrderByHYCService;

    @Autowired
    private HycExceptionMsgMapper hycExceptionMsgMapper;

    @Autowired
    private SoshiplinebackhycMapper soshiplinebackhycMapper;
    @Autowired
    private SoshiplineMapper soshiplineMapper;

    //对异常数据进行处理
    @Override
    public void doDisposeExe() throws Exception {
        //1、获取到所有未处理的异常信息
        List<HycExceptionMsg> msgList =  hycExceptionMsgMapper.getUndisposedMsg();
        if(CollectionUtils.isNotEmpty(msgList)){
            for(int i = 0; i < msgList.size(); i++){
                HycExceptionMsg msg = msgList.get(i);
                //2、将异常异常信息根据类型进行区分
                if(msg.getExceptionType() == CallHYCUrlTypeEnum.HYC_WAYBILLDETAILS.getValue()){
                    //运单出现了异常
                    //清理之前残留的数据
                    try{
                        callWayBillByHYCService.saveWayBillDetails(msg.getExceptionData());
                        msg.setIsdispose(1L);
                        msg.setDtupdate(new Date());
                        hycExceptionMsgMapper.updateByPrimaryKey(msg);
                    }catch (Exception e){

                        LOGGER.error("异常运单处理异常："+msg.getExceptionData() +"异常信息" + e.toString());
                   }
                }
            }
        }
    }

    /**
     * 处理指定异常运单
     * @param msgCode  运单号
     * @throws Exception
     */
    @Override
    public void doDisposeExeByCode(String msgCode) throws Exception{
        try{
            HycExceptionMsg hycExceptionMsg = hycExceptionMsgMapper.getMsgByMsgCode(msgCode);

            if(!Objects.isNull(hycExceptionMsg)){
                callWayBillByHYCService.saveWayBillDetails(msgCode);
                //获取异常订单的信息
                hycExceptionMsg.setIsdispose(1L);
                hycExceptionMsg.setDtupdate(new Date());
                hycExceptionMsgMapper.updateByPrimaryKey(hycExceptionMsg);
            }else{
                LOGGER.error("未找到对应的异常运单:"+msgCode);
            }

        }catch (Exception e){
            LOGGER.error("异常运单处理异常："+msgCode +"异常信息" + e.toString());
            throw e;
        }
    }

    @Override
    public void saveExecption(String exceptionMsg,String exceptionData,Integer exceptionType) throws Exception {
        try{
            //检查异常是否已经存在
            HycExceptionMsg msg = new HycExceptionMsg();
            msg.setExceptionMsg(exceptionMsg);
            msg.setExceptionData(exceptionData);
            msg.setExceptionType(exceptionType);
            //检查异常是否已存在
            Integer sign = hycExceptionMsgMapper.checkException(msg);
            if( sign == 0 ){
                hycExceptionMsgMapper.saveMsgByHYC(msg);
            }
        }catch (Exception e){
            throw new Exception(e);
        }
    }

    /**
     * 数据修改
     * @throws Exception
     */
    @Override
    public void dateAmend() throws Exception {
        try{
            //获取修改的数据
            List<Soshiplinebackhyc> soshiplinebackhycList = soshiplinebackhycMapper.getAll();
            if(CollectionUtils.isNotEmpty(soshiplinebackhycList)){
                for(Soshiplinebackhyc soshiplinebackhyc : soshiplinebackhycList){
                    Soshipline soshipline = new Soshipline();
                    soshipline.setIpaycosttype(1);
                    soshipline.setDcpaycost(soshiplinebackhyc.getUnitcost());
                    soshipline.setIlineid(soshiplinebackhyc.getIlineid());
                    soshiplineMapper.updatePaycost(soshipline);
                }
            }
        }catch (Exception e){
            throw new Exception();
        }
    }

    @Override
    public List<HycExceptionMsgBo> getUndisposedExceptionList() {
        //1、获取到所有未处理的异常信息
        List<HycExceptionMsgBo> msgList =  hycExceptionMsgMapper.getUndisposedExceptionList();
        return  msgList;
    }

    @Override
    public Boolean doDisposeExeByOrderCode(String orderCode) throws Exception {
        Boolean blnRtn =false;
        try{
            HycExceptionMsg hycExceptionMsg = hycExceptionMsgMapper.getMsgByMsgCode(orderCode);

            if(!Objects.isNull(hycExceptionMsg)){
                callOrderByHYCService.saveOrderByCode(orderCode);
                //获取异常订单的信息
                hycExceptionMsg.setIsdispose(1L);
                hycExceptionMsg.setDtupdate(new Date());
                hycExceptionMsgMapper.updateByPrimaryKey(hycExceptionMsg);
            }else{
                LOGGER.error("未找到对应的异常订单:"+orderCode);
            }

        }catch (Exception e){
            LOGGER.error("异常订单处理异常："+orderCode +"异常信息" + e.getMessage());
            throw e;
        }

        return  blnRtn;
    }

    @Override
    public Boolean doDisposeExe(int type, String code) throws Exception {
        Boolean blnRtn =false;
        try{
            if(type == CallHYCUrlTypeEnum.HYC_ORDERDETAILS.getValue()){
                blnRtn = doDisposeExeByOrderCode(code);
            }
            if(type ==  CallHYCUrlTypeEnum.HYC_WAYBILLDETAILS.getValue()){
                doDisposeExeByCode(code);
                blnRtn = true;
            }
        }catch (Exception e){
            throw e;
        }
        return  blnRtn;
    }
}
